//app.js

var httpClient = require('utils/httpClient');
var route = require('utils/route');
var config = require('/config/config');

App({
  onLaunch: function () {
    var value = wx.getStorageSync('user');
    if (value != null && value != '') {
      var that = this;
      that.globalData.userInfo = value;
      that.globalData.openId = value.openid;
      that.globalData.userId = value.id;
      if (value) {
        wx.navigateTo({
          url: "pages/index/index"
        })
      }
    }
  },
  getUserInfo: function (cb) {
    var value = wx.getStorageSync('user');
    if (value == null || value == '') {
      var that = this;
      wx.login({
        success: function (loginCode) {
          console.log(loginCode);
          var method = route.registerUser();
          var params = {};
          params['code'] = loginCode.code;
          httpClient.request(method, 'POST', params, function (res, err) {
            that.globalData.userInfo = res;
            that.globalData.openId = res.openid;
            that.globalData.userId = res.id;
            wx.setStorage({
              key: "user",
              data: res
            });
            wx.setStorage({
              key: "mask",
              data: 0
            });
          });
        }, fail: function () {
          that.openSetting(cb);
        }
      })
    } else {
      this.globalData.userInfo = value;
      this.globalData.openId = value.openid;
      this.globalData.userId = value.id;
      typeof cb == "function" && cb(value);
    }
  },
  //授权方法
  getUser: function (options) {
    var that = this;
    var errMsg = options.detail.errMsg;
    if (errMsg == 'getUserInfo:ok') {
      wx.login({
        success: function (loginCode) {
          console.log(loginCode);
          var method = route.registerUser();
          var params = options.detail.userInfo;
          params.code = loginCode.code;
          httpClient.request(method, 'POST', params, function (res, err) {
            that.globalData.userInfo = res;
            that.globalData.openId = res.openid;
            that.globalData.userId = res.id;
            wx.setStorage({
              key: "user",
              data: res
            });
            wx.setStorage({
              key: "mask",
              data: 0
            });
            if (res) {
              if (that.globalData.tradeNo) { 
                wx.redirectTo({
                  url: '../guesspacket/guesspacket?tradeNo=' + that.globalData.tradeNo
                });  
              } else {
                wx.redirectTo({
                  url: '../index/index'
                })
              }
            }
          });
        }
      });
    }
  },
  openSetting: function (cb) {
    var that = this;
    wx.openSetting({
      success: (res) => {
        debugger
        console.log("授权结果.." + res)
        if (!res.authSetting["scope.userInfo"]) {
          that.getUserInfo(cb);
        }
      }
    })
  },
  auth: function () {
    var that = this;
    var method = route.getAuth();
    var params = {
      userName: "admin",
      password: "admin"
    }
    httpClient.request(method, 'POST', params, function (res, err) {
      that.config.token = res.data.token;
    });
  },  
  globalData: {
    userInfo: null,
    openId: '',
    session: '',
    userId: '',
    quesNo: '',
    quesName: '',
    titleCassification: '', 
  }

})